#include "head.h"

int changedir(user_t* user, char* path, int net_fd)
{
//printf("cd: %s\n", path);
    char buffer[1024] = {'\0'};
    strcpy(buffer, user->path);

    int size = strlen(user->path);
    char* tmp = user->path + 1;
    while(*tmp != '/'){
        tmp++;
    }
    if(strcmp(path, "..") == 0){
        if(*(tmp+1) == '\0'){
            printf("没有上层目录\n");
            return 1;
        }
        else{
            tmp = user->path + size - 2;
            while(*tmp != '/'){
                *tmp-- = '\0';      
            }
            return 0;
        }
    }
    if(strcmp(path, ".") == 0){
        return 0;
    }
    tmp = user->path + size;
    //*tmp++ = '/';
    while(*tmp++ = *path++)
        ;
    *(tmp-1) = '/';
    *tmp = '\0';
//printf("cd: %s\n", user->path);
    //TODO: send recv;
//printf("我也在这里\n");    

    int type = 0;
    recv(net_fd, &type, sizeof(int), MSG_WAITALL);
    if(type == 0 || type == -1){
        memcpy(user->path, buffer, sizeof(buffer));
        return 1;
    }

    return 0;
}
